package wsp.dailymarket.shopping;

import java.text.NumberFormat;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import wsp.dailymarket.deals.DealBean;

/**
 * Represents a line item on the invoice
 * @author Matt
 */
@ManagedBean(name="lineItemBean")
@RequestScoped
public class InvoiceLineItem {

	private int lineItemId;
	private DealBean theDeal;
	private int status;
	private ArrayList<InvoiceDealCode> codes;

	public static final int STATUS_INVALID = 0x0E00;   // This object does not contain a valid line item from an invoice
	public static final int STATUS_COMPLETE = 0x0E01;  // This line item is complete and codes are valid
	public static final int STATUS_PENDING = 0x0E02;   // This line item is pending the completion of a timed deal
	public static final int STATUS_RETRACTED = 0x0E11; // This line item has been retracted because the deal did not make its min or max quantities
	public static final int STATUS_CANCELLED = 0x0E12; // This line item was cancelled by the customer

	public InvoiceLineItem() {
		lineItemId = 0;
		codes = new ArrayList<InvoiceDealCode>();
		theDeal = new DealBean();
		status = STATUS_INVALID;
	}

	public void setLineItemId(int newValue) {
		this.lineItemId = newValue;
	}

	public int getLineItemId() {
		return this.lineItemId;
	}

	public ArrayList<InvoiceDealCode> getCodes() {
		return this.codes;
	}

	public void setDeal(DealBean newValue) {
		this.theDeal = newValue;
	}

	public DealBean getDeal() {
		return this.theDeal;
	}

	public int getQuantity() {
		return this.codes.size();
	}

	public void setStatus(int newValue) {
		this.status = newValue;
	}

	public int getStatus() {
		return this.status;
	}

	public String getStatusString() {
		switch (this.status) {
			case InvoiceLineItem.STATUS_COMPLETE:
				return "Complete";
			case InvoiceLineItem.STATUS_PENDING:
				return "Pending";
			case InvoiceLineItem.STATUS_RETRACTED:
				return "Retracted";
			case InvoiceLineItem.STATUS_CANCELLED:
				return "Cancelled";
			case InvoiceLineItem.STATUS_INVALID:
			default:
				return "Invalid";
		}
	}

	public String getStatusStyle() {
		switch (this.status) {
			case InvoiceLineItem.STATUS_COMPLETE:
				return "status good";
			case InvoiceLineItem.STATUS_PENDING:
				return "status warn";
			case InvoiceLineItem.STATUS_RETRACTED:
			case InvoiceLineItem.STATUS_CANCELLED:
				return "status error";
			case InvoiceLineItem.STATUS_INVALID:
			default:
				return "status fatal";
		}
	}

	public float getTotalLineItemPrice() {
		return this.theDeal.getPrice() * this.getQuantity();
	}

	public String getTotalLineItemPriceDisplay() {
		return NumberFormat.getCurrencyInstance().format(this.getTotalLineItemPrice());
	}

	public boolean getHasCodes() {
		return !this.codes.isEmpty();
	}

	public int getNumCodes() {
		return this.codes.size();
	}

}
